<?php

declare(strict_types=1);
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2022/05/20
//      |_|                                         | TpMeCMS

namespace ImiApp\ApiServer\Api\Controller\com;

use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
// use ImiApp\ApiServer\Common\AuthApi;
use Phpben\Imi\Auth\Annotation\Auth;



use Imi\Db\Db;
use ImiApp\ApiServer\Api\Controller\Tpmecms;



/**
 * 分享
 * @Auth(name="token",nologin={"get_data","register","resetpwd","test"})
 * @Controller("/com.share/")
 */
class Share extends Tpmecms
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];



    /**
     * 获取分享详情
     *
     * @param string|null $url 链接
     * @param string|null $code 代码
     * @param string|null $content 内容
     * @return void
     * @Action
     */
    public function get_data(?string $url = null, ?string $code = null, ?string $content = null)
    {
        if ($url) {
            $where['url'] = $url;
        }

        if ($code) {
            $where['code'] = $code;
        }

        if ($content) {
            $where['content'] = ['like', "%" . $content . "%"];
        }

        if ($url || $code || $content) {
            
        } else {
            return $this->error('请传递参数');
        }

        $res = Db::name('share')->whereEx($where)->find();

        if ($res) {
            switch ($res['type']) {
                case 'auction_help':
                    $res['auction'] = Db::name('auction')->where('id', '=', $res['public_id'])->find();
                    $params = json_decode($res['params'], true);
                    if ($params) {
                        $goodssku_price_id = $params['goods_sku_price_id'];

                        // $goodssku_price_id = Db::name('offer')->where('user_id','=',$res['user_id'])->where('auction_id','=',$res['public_id'])->value('goods_sku_price_id');
                        $res['goods_sku_price'] = Db::name('goodssku_price')->where('id', '=', $goodssku_price_id)->fieldRaw('id,goods_sku_ids,image,price,goods_sku_text,stock')->find();
                    }
                    break;

                default:
                    # code...
                    break;
            }
        }

        // $data['switch'] = 1;
        // $data['id'] = $id;

        // $res = Db::name('artcle')->whereEx($data)->find();
        // if (!$res) {
        //     return $this->error('没有该信息');
        // }
        // $this->ChangeFieldData($res);
        // $this->AddViews($id);

        return $this->success('ok', $res);
    }

    // /**
    //  * 增加浏览量
    //  *
    //  * @param int $id
    //  * @return void
    //  */
    // protected function AddViews($id)
    // {
    //     Db::name('artcle')->where('id','=', $id)->setFieldInc('views')->update();
    //     if ($this->auth->isLogin())
    //         $this->AddViewlog(ARTCLE, $id, $this->auth->user()->id);
    // }



    // /**
    //  * 改变通知字段
    //  *
    //  * @param array $val 
    //  * @return void
    //  */
    // protected function ChangeFieldData(&$val)
    // {
    //     $val['cate'] = Db::name('artclecate')->where('id', '=',$val['artclecate_id'])->value('name');
    //     $val['date_str'] = $this->TimeToStr($val['createtime']);
    //     return $val;
    // }
}
